import PyPDF2
import re
import os
import csv
import pandas as pd
import time
# 该函数用于开启自动阅读器
def start(file_path):
    PDFfile_names = PDF_names(file_path)
    return PDFfile_names
# 该函数用于获取目标文件夹中所有PDF文件的名称并存入列表中
def PDF_names(file_path):
    nameset = os.listdir(file_path)
    print("-------开始扫描文件夹中的文件信息-----------")
    PDF_names = []
    for file_names in nameset:
        if ".pdf" in file_names:
            PDF_names.append(file_names)
    print("-------完成扫描！即将开始读取文本！----------")
    return PDF_names
# 该函数用于开启并阅读PDF文件,获取全部的文本信息,返回包含全部文本的长字符串
def PDF_readtext(PDF_filename):
    f = open(PDF_filename, 'rb')
    # pdfFileReader从pdf读取文本
    print("-------开始读取"+PDF_filename+"文件中的文本内容-----------")
    pdf_reader = PyPDF2.PdfReader(f)
    # getPage读取特定页面的文本。
    pdf_text = []
    for p in range(len(pdf_reader.pages)):
        page = pdf_reader.pages[p]

        pdf_text.append(page.extract_text())
    #     接下来是对读取的文本进行预处理
    PDF_text = ','.join(pdf_text).strip("\n")
    PDF_text = PDF_text.replace('\n', '')
    print("-------开始读取成功！-----------")
    return PDF_text
# 该函数负责使用正则表达式来读取票据业务的信息
def PIAOJV(str):
    a = re.findall('发生票据业务(.*?)。', str)
    if a == []:
        print("此处报错！！！")
        return "NULL"
    reply = "票据业务查询结果："+a[0]
    print(reply)
    return a[0]
def ZHIPIAO(str):
    b = re.findall('支票业务(.*?)；', str)
    if b == []:
        print("此处报错！！！")
        return "NULL"
    reply = "支票业务查询结果：" + b[0]
    print(reply)
    return b[0]
def SHANGYEHUIPIAO(str):
    # 实际结算商业汇票业务
    b = re.findall('实际结算商业汇票业务(.*?)；', str)
    if b == []:
        print("此处报错！！！")
        return "NULL"
    reply = "实际结算商业汇票业务查询结果：" + b[0]
    print(reply)
    return b[0]
def YINGHANGHUIPIAO(str):
    d = re.findall('银行汇票业务(.*?)；', str)
    if d == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行汇票业务查询结果："+d[0]
    print(reply)
    return d[0]
def YINGHANGBENPIAO(str):
    e = re.findall('银行本票业务(.*?)。', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行本票业务查询结果：" + e[0]
    print(reply)
    return e[0]
def DIANZISHANGYEHUIPIAOXITONG(str):
    e = re.findall('电子商业汇票系统出票(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "电子商业汇票系统出票查询结果：" + e[0]
    print(reply)
    return e[0]
def CHENGDUI(str):
    e = re.findall('承兑(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "承兑查询结果：" + e[0]
    print(reply)
    return e[0]
def TIEXIAN(str):
    e = re.findall('贴现(.*?)1自', str)
    if e == []:
        e = re.findall('贴现(.*?)；转贴现', str)
        reply = "贴现查询结果：" + e[0]
        print(reply)
        return e[0]
    e += re.findall('信用证业务。,(.*?)；转贴现', str)
    f = ','.join(e).replace(',', '')
    reply = "贴现查询结果：" + f
    print(reply)
    return f
def ZHUANTIEXIAN(str):
    e = re.findall('；转贴现(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "转贴现查询结果：" + e[0]
    print(reply)
    return e[0]
def ZHIYASHIHUIGOU(str):
    e = re.findall('质押式回购(.*?)3。', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "质押式回购查询结果：" + e[0]
    print(reply)
    return e[0]
def YINGHANGKAJIAOYI(str):
    e = re.findall('发生银行卡交易6(.*?)。', str)
    if e == []:
        e = re.findall('发生银行卡交易(.*?)。', str)
        if e == []:
            print("银行卡交易查询异常！！！！")
            e = re.findall('银行卡交易(.*?)。', str)
            print(e)
            return ','.join(e).replace(',','')
        reply = "银行卡交易查询结果：" + e[0]
        print(reply)
        return e[0]
    else:
        reply = "银行卡交易查询结果：" + e[0]
        print(reply)
        return e[0]
def CUNXIAN(str):
    e = re.findall('存现业务(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行卡存现业务查询结果：" + e[0]
    print(reply)
    return e[0]
def QVNXIAN(str):
    e = re.findall('取现业务(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行卡取现业务查询结果：" + e[0]
    print(reply)
    return e[0]
def XIAOFEI(str):
    e = re.findall('消费业务(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行卡消费业务查询结果：" + e[0]
    print(reply)
    return e[0]
def ZHUANZHANG(str):
    e = re.findall('转账业务(.*?)；', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "银行卡转账业务查询结果：" + e[0]
    print(reply)
    return e[0]
def ZA(str):
    e = re.findall('贷记转账、直接借记、托收承付以及国内信用证结算业务(.*?)。', str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "贷记转账、直接借记、托收承付以及国内信用证结算业务查询结果：" + e[0]
    print(reply)
    return e[0]
def DIANZIZHIFU(str):
    e = re.findall('电子支付9业务(.*?)。', str)
    if e == []:
        e = re.findall('电子支付业务(.*?)。', str)
        if e == []:
            print("此处报错！！！")
            return "NULL"
        reply = "电子支付业务查询结果：" + e[0]
        print(reply)
        return e[0]
    reply = "电子支付业务查询结果：" + e[0]
    print(reply)
    return e[0]
def WANGSHANGZHIFU(str):
    e = re.findall('网上支付业务(.*?)；',str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "网上支付业务查询结果：" + e[0]
    print(reply)
    return e[0]
def YIDONGZHIFU(str):
    e = re.findall('；移动支付业务(.*?)；',str)
    if e == []:
        print("此处报错！！！")
        return "NULL"
    reply = "移动支付业务查询结果：" + e[0]
    print(reply)
    return e[0]
def DIANHUAZHIFU(str):
    e = re.findall('电话支付业务(.*?)；',str)
    if e == []:
        print("此处报错！！！！")
        return "NULL"
    reply = "电话支付业务查询结果：" + e[0]
    print(reply)
    return e[0]
start_time = time.time()
filePath = 'C:\\Users\\徐林鑫\\Untitled Folder'
names = []
names = start(filePath)
# 设计读取结果的字典，要求包含文件的名称，存储每个函数的返回值
resultdict = ['文件名', "票据业务",
              "支票业务","商业汇票业务",
              "银行汇票业务","银行本票业务",
              "电子商业汇票业务","承兑",
              "贴现","质押式回购",
              "银行卡交易","取现",
              "转账","消费",
              "存现","转贴现",
              "贷记转账、直接借记、托收承付以及国内信用证结算业务","电子支付",
              "网上支付","移动支付",
              "电话支付"]
filename = []
PIAOJV_L = []
ZHIPIAO_L = []
SHANGYEHUIPIAO_L = []
YINGHANGHUIPIAO_L = []
YINGHANGBENPIAO_L =[]
DIANZISHANGYEHUIPIAOXITONG_L = []
CHENGDUI_L = []
TIEXIAN_L = []
ZHIYASHIHUIGOU_L = []
YINGHANGKAJIAOYI_L = []
QVNXIAN_L = []
ZHUANZHANG_L = []
XIAOFEI_L = []
CUNXIAN_l = []
ZHUANTIEXIAN_L = []
ZA_L = []
DIANZIZHIFU_L = []
WANGSHANGZHIFU_L = []
YIDONGZHIFU_L  = []
DIANHUAZHIFU_L = []

for name in names:
    PDF_text = PDF_readtext(name)
    filename.append(name)
    PIAOJV_L.append(PIAOJV(PDF_text))
    ZHIPIAO_L.append(ZHIPIAO(PDF_text))
    SHANGYEHUIPIAO_L.append(SHANGYEHUIPIAO(PDF_text))
    YINGHANGHUIPIAO_L.append(YINGHANGHUIPIAO(PDF_text))
    YINGHANGBENPIAO_L.append(YINGHANGBENPIAO(PDF_text))
    DIANZISHANGYEHUIPIAOXITONG_L.append(DIANZISHANGYEHUIPIAOXITONG(PDF_text))
    CHENGDUI_L.append(CHENGDUI(PDF_text))
    TIEXIAN_L.append(TIEXIAN(PDF_text))
    ZHIYASHIHUIGOU_L.append(ZHIYASHIHUIGOU(PDF_text))
    YINGHANGKAJIAOYI_L.append(YINGHANGKAJIAOYI(PDF_text))
    QVNXIAN_L.append(QVNXIAN(PDF_text))
    ZHUANZHANG_L.append(ZHUANZHANG(PDF_text))
    XIAOFEI_L.append(XIAOFEI(PDF_text))
    CUNXIAN_l.append(CUNXIAN(PDF_text))
    ZHUANTIEXIAN_L.append(ZHUANTIEXIAN(PDF_text))
    ZA_L.append(ZA(PDF_text))
    DIANZIZHIFU_L.append(DIANZIZHIFU(PDF_text))
    WANGSHANGZHIFU_L.append(WANGSHANGZHIFU(PDF_text))
    YIDONGZHIFU_L.append(YIDONGZHIFU(PDF_text))
    DIANHUAZHIFU_L.append(DIANHUAZHIFU(PDF_text))
column = resultdict
list = []
list .append(filename)
list.append(PIAOJV_L)
list.append(ZHIPIAO_L)
list.append(SHANGYEHUIPIAO_L)
list.append(YINGHANGHUIPIAO_L)
list.append(YINGHANGBENPIAO_L)
list.append(DIANZISHANGYEHUIPIAOXITONG_L)
list.append(CHENGDUI_L)
list.append(TIEXIAN_L)
list.append(ZHIYASHIHUIGOU_L)
list.append(YINGHANGKAJIAOYI_L)
list.append(QVNXIAN_L)
list.append(ZHUANZHANG_L)
list.append(XIAOFEI_L)
list.append(CUNXIAN_l)
list.append(ZHUANTIEXIAN_L)
list.append(ZA_L)
list.append(DIANZIZHIFU_L)
list.append(WANGSHANGZHIFU_L)
list.append(YIDONGZHIFU_L)
list.append(DIANHUAZHIFU_L)
data1 = pd.DataFrame(list).T
print(data1.columns)
data1.columns = column
print(data1)
data1.to_csv('test.csv')
time_end = time.time()
print('Running time: %s Seconds'%(time_end-start_time))



